﻿namespace Pp.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Core;
    using Core.Extentions;
    using Core.File;
    using Domain.Model;
    using Dto;
    using Globalization;
    using NHibernate;
    using NHibernate.Criterion;

    public class CommentRepository : BaseRepository<Comment>
    {
        private readonly UserRepository _userRepository;
        private readonly ProjectRepository _projectRepository;

        public CommentRepository(ISession session, HttpServerUtilityBase server)
            : base(session, server)
        {
            _userRepository = new UserRepository(session, server);
            _projectRepository = new ProjectRepository(session, server);

        }

        public Result NewComment(string comment, Guid? projectId, string username)
        {
            var user = _userRepository.GetByUsername(username);
            var project = _projectRepository.Get(projectId);
            if (user == null) throw new Exception(Res.Information_Cannot_make_comment_before_login);
            if (project == null) throw new Exception(Res.Information_Cannot_make_comment_before_select_project);
            var cmmt = new Comment
                           {
                               IsActive = true,
                               CommentText = comment,
                               User = user,
                               Project = project,
                               IsShowed = false
                           };
            Save(cmmt);
            var message = CreateMail(Res.Information_Project_notification, MailType.ProjectComment, new List<MailModel>
                                                                                  {
                                                                                      new MailModel("ProjectName", project.Name),
                                                                                      new MailModel("ProjectId", project.Id.ToString()),
                                                                                      new MailModel("CommentWriter",user.GetFullName()),
                                                                                      new MailModel("CommentText",cmmt.CommentText)
                                                                                  }, project.User.MailAdress).Data;
            foreach (var projectFollow in project.Follows)
                message.To.Add(projectFollow.Follower.MailAdress);

            SendMail(message);
            return Result.AsSuccess();
        }

        public PageList<CommentDto> AllComment(int? page, int pageSize)
        {
            var pageStart = page.HasValue ? page.Value : 0;
            var maxCount = Session.CreateCriteria(typeof(Comment)).SetProjection(Projections.Count("Id")).UniqueResult<int>();
            var comments = Session.CreateCriteria(typeof(Comment))
                .AddOrder(Order.Desc("CreateDate"))
                .SetFirstResult(pageStart * pageSize)
                .SetMaxResults(pageSize)
                .List<Comment>();
            var list = comments.Select(c => c.ToDto()).ToList();
            return new PageList<CommentDto> { List = list, PageInfo = new PageInfo { ListCount = maxCount, PageNumber = pageStart, PageSize = pageSize, UrlPrefix = "/Home/Index" } };

        }

        public Result DeleteById(Guid? id)
        {
            var com = Get(id);
            if (com == null) Result.AsError(Res.Information_Comment_nofound_for_remove);
            Delete(com);
            return Result.AsSuccess();
        }
    }
}
